/**
 * kl yy-ds @2021
 */
package com.cxps.server.menu.controller;

import com.cxps.cloud.message.ApiResponse;
import com.cxps.server.menu.domain.MenuForm;
import com.cxps.server.menu.domain.MenuQuery;
import com.cxps.server.menu.service.MenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

/**
 * 菜单控制层
 *
 * @author YouCai.Liu
 * @since 2021/10/12
 * ...
 */
@Slf4j
@RestController
@RequestMapping("/admin/menu")
@Api(tags = "菜单API")
public class MenuController {

    @Autowired
    private MenuService menuService;

    /**
     * 菜单列表
     *
     * @param query
     * @return
     */
    @PostMapping("/list")
    @ApiOperation(value = "菜单列表", response = ApiResponse.class)
    public ApiResponse getMenuList(@ApiParam(name = "query", value = "检索条件", required = true)
                                   @RequestBody MenuQuery query) {
        return ApiResponse.fireSuccess(menuService.getMenuList(query));
    }

    /**
     * 根据菜单ID查询数据
     *
     * @param id
     * @return
     */
    @GetMapping("/get/{id}")
    @ApiOperation(value = "根据菜单ID查询数据", response = ApiResponse.class)
    public ApiResponse getMenuById(@ApiParam(name = "id", value = "菜单ID", required = true)
                                   @PathVariable String id) {
        return ApiResponse.fireSuccess(menuService.getMenuById(id));
    }

    /**
     * 添加菜单
     *
     * @param form
     * @return
     */
    @PostMapping("/add")
    @ApiOperation(value = "添加菜单", response = ApiResponse.class)
    public ApiResponse addMenu(@ApiParam(name = "form", value = "添加表单", required = true)
                               @RequestBody MenuForm form) {
        try {
            Assert.notNull(form, "form can not be null");
            menuService.addMenu(form);
            return ApiResponse.fireSuccess();
        } catch (IllegalArgumentException e) {
            log.error(e.getMessage(), e);
            return ApiResponse.fireError();
        }
    }

    /**
     * 修改菜单
     *
     * @param form
     * @return
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改菜单", response = ApiResponse.class)
    public ApiResponse updateMenu(@ApiParam(name = "form", value = "修改表单", required = true)
                                  @RequestBody MenuForm form) {
        try {
            Assert.notNull(form, "form can not be null");
            Assert.hasLength(form.getId(), "id can not be null");
            menuService.updateMenu(form);
            return ApiResponse.fireSuccess();
        } catch (IllegalArgumentException e) {
            log.error(e.getMessage(), e);
            return ApiResponse.fireError();
        }
    }

    /**
     * 根据菜单ID删除单个数据
     *
     * @param id
     * @return
     */
    @GetMapping("/del/{id}")
    @ApiOperation(value = "根据菜单ID删除单个数据", response = ApiResponse.class)
    public ApiResponse delMenuById(@ApiParam(name = "id", value = "菜单ID", required = true)
                                   @PathVariable String id) {
        menuService.delMenuById(id);
        return ApiResponse.fireSuccess();
    }

    /**
     * 根据菜单ID删除多个数据
     *
     * @param ids
     * @return
     */
    @GetMapping("/del")
    @ApiOperation(value = "根据菜单ID删除多个数据", response = ApiResponse.class)
    public ApiResponse delMenuByIds(@ApiParam(name = "ids", value = "菜单ID集合", required = true)
                                    @RequestParam String[] ids) {
        try {
            Assert.notNull(ids, "ids can not be null");
            menuService.delMenuByIds(ids);
            return ApiResponse.fireSuccess();
        } catch (IllegalArgumentException e) {
            log.error(e.getMessage(), e);
            return ApiResponse.fireError();
        }
    }

    /**
     * 获取上级菜单
     *
     * @return
     */
    @GetMapping("/info")
    @ApiOperation(value = "获取上级菜单", response = ApiResponse.class)
    public ApiResponse getParentMenu() {
        return ApiResponse.fireSuccess(menuService.getParentMenu());
    }

    /**
     * 获取树形结构菜单数据
     *
     * @return
     */
    @GetMapping("/tree")
    @ApiOperation(value = "获取树形结构菜单数据", response = ApiResponse.class)
    public ApiResponse getMenuTree() {
        return ApiResponse.fireSuccess(menuService.getMenuTree());
    }


}
